package com.xxgc.vfc.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Types;
import java.util.Collections;

/**
 * @Author: SJY
 * @Date: 2023/11/7 14:45:57
 * 微服务工程逆向生成器
 */
public class MyBatisGenerator {
    //--------------基本配置--------------
    private static String url = "jdbc:mysql://localhost:3306/vfc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
    private static String username = "root";
    private static String password = "123456";
    private static String author = "风清杨";
    private static String packageUrl = "com.xxgc.vfc";
    private static String modelName = null;

    //------------经常修改的内容------------
    //生成的表名
    private static String tableName = "vfc_user_and_roles";
    //表的前缀
    private static String tablePrefix = "vfc_";
    //-------------服务的模块相关配置----------
    private static String modelPackage = "auth";
    //模块名
    //private static String modulesName = "\\volunteer-filling-modules\\";
    private static String modulesName = "";
    //服务名
    //private static String serviceName = "volunteer-filling-modules-provider\\volunteer-filling-modules-provider-basic-info\\";
    private static String serviceName = "\\volunteer-filling-auth";


    public static void main(String[] args) {
        //拼接工程包和服务包
        packageUrl = packageUrl + "." + modelPackage;

        //获取当前项目路径(Java)
        String systemUrlJava = System.getProperty("user.dir")+modulesName+serviceName+"\\src\\main\\java\\";
        //获取当前项目路径(Mapper文件)
        String systemUrlMapper = System.getProperty("user.dir")+modulesName+serviceName+"\\src\\main\\resources\\mapper\\";

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(systemUrlJava); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent(packageUrl) // 设置父包名
                            .moduleName(modelName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, systemUrlMapper)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix(tablePrefix)// 设置过滤表前缀
                            .entityBuilder()
                            .enableFileOverride();//覆盖生成的entity

                    builder.entityBuilder()
                            //逻辑删除的字段名
                            .logicDeleteColumnName("flag")
                            //组件类型
                            .idType(IdType.ASSIGN_ID)
                            .addTableFills(
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.INSERT_UPDATE)
                            )
                            //开启lombok
                            .enableLombok();

                })
                //模板
                .templateConfig(builder -> {
                    //不生成controller
                    builder.disable(TemplateType.CONTROLLER);
                    //不生成service
                    builder.disable(TemplateType.SERVICE);
                    //不生成service的impl
                    builder.disable(TemplateType.SERVICE_IMPL);
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
